package org.nlp2rdf.navigator.util;

import org.apache.log4j.Logger;
import org.nlp2rdf.navigator.client.exception.NKERuntimeException;

public class ExceptionHelper {
	private static final Logger logger = Logger.getLogger(ExceptionHelper.class);

	/**
	 * 
	 * Catches and re-throws a runtime exception
	 * 
	 * @param runtimeException
	 * @param clazz
	 * @return
	 * @throws NKERuntimeException
	 */
	@SuppressWarnings("unchecked")
	public static String handleRuntimeException(RuntimeException runtimeException, Class clazz)
			throws NKERuntimeException {
		StringBuffer message = new StringBuffer("Error caught in " + clazz.getSimpleName());
		StackTraceElement[] arr = runtimeException.getStackTrace();
		for (int x = 0; x < arr.length; x++) {
			String current = arr[x].toString() + "\n";
			if (x < 10) {
				message.append(current);
			}
		}
		message.append("cut see server log ");
		logger.error("Error caught in " + clazz + "", runtimeException);
		throw new NKERuntimeException(message.toString());
	}
}
